<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 2] 2.6 Primitive Data Types</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 15:47:46 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 2<br>How Java Differs from C</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch02_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-2-SECT-6">2.6 Primitive Data Types</A></h2>

<P CLASS=para>
<A NAME="PRIMITIVE-DATA-TYPES"></A><A NAME="DATA-TYPES"></A><A NAME="BYTE-DATA-TYPE"></A>Java adds <tt CLASS=literal>byte</tt> and <tt CLASS=literal>boolean</tt> primitive types
to the standard set of C types.  In addition, it strictly
defines the size and signedness of its types.  In
C, an <tt CLASS=literal>int</tt> may be 16, 32, or 64 bits, and a
<tt CLASS=literal>char</tt> may act signed or unsigned depending on the
platform.  Not so in Java.  In C, an uninitialized local
variable usually has garbage as its value.  
In Java, all variables have guaranteed default values, though the
compiler may warn you in places where you rely, accidentally
or not, on these default values.
<A HREF="ch02_06.htm#JNUT2-CH-2-TAB-2">Table 2.2</A>
lists Java's primitive data types.  The subsections below provide
details about these types.

<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="JNUT2-CH-2-TAB-2">Table 2.2: Java Primitive Data Types</A></CAPTION>
<TR CLASS=row>
<TH ALIGN="left">&nbsp;</TH>
<TH ALIGN="left">&nbsp;</TH>
<TH ALIGN="left">&nbsp;</TH>
<TH ALIGN="left">&nbsp;</TH>
<TH ALIGN="left">Min Value</TH>
</TR>
<TR CLASS=row>
<TH ALIGN="left">Type</TH>
<TH ALIGN="left">Contains</TH>
<TH ALIGN="left">Default</TH>
<TH ALIGN="left">Size</TH>
<TH ALIGN="left">Max Value</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left"><tt CLASS=literal>true</tt> or <tt CLASS=literal>false</tt></TD>
<TD ALIGN="left"><tt CLASS=literal>false</tt></TD>
<TD ALIGN="left">1 bit</TD>
<TD ALIGN="left">N.A.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">N.A.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">char</TD>
<TD ALIGN="left">Unicode character</TD>
<TD ALIGN="left"><tt CLASS=literal>\u0000</tt></TD>
<TD ALIGN="left">16 bits</TD>
<TD ALIGN="left"><tt CLASS=literal>\u0000</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left"><tt CLASS=literal>\uFFFF</tt></TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">byte</TD>
<TD ALIGN="left">signed integer</TD>
<TD ALIGN="left">0</TD>
<TD ALIGN="left">8 bits</TD>
<TD ALIGN="left">-128</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">127</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">short</TD>
<TD ALIGN="left">signed integer</TD>
<TD ALIGN="left">0</TD>
<TD ALIGN="left">16 bits</TD>
<TD ALIGN="left">-32768</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">32767</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">int</TD>
<TD ALIGN="left">signed integer</TD>
<TD ALIGN="left">0</TD>
<TD ALIGN="left">32 bits</TD>
<TD ALIGN="left">-2147483648</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">2147483647</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">long</TD>
<TD ALIGN="left">signed integer</TD>
<TD ALIGN="left">0</TD>
<TD ALIGN="left">64 bits</TD>
<TD ALIGN="left">-9223372036854775808</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">9223372036854775807</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">float</TD>
<TD ALIGN="left">IEEE 754</TD>
<TD ALIGN="left">0.0</TD>
<TD ALIGN="left">32 bits</TD>
<TD ALIGN="left">+/-3.40282347E+38</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">floating-point</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">+/-1.40239846E-45</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">double</TD>
<TD ALIGN="left">IEEE 754</TD>
<TD ALIGN="left">0.0</TD>
<TD ALIGN="left">64 bits</TD>
<TD ALIGN="left">+/-1.79769313486231570E+308</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">floating-point</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">+/-4.94065645841246544E-324</TD>
</TR>
</TABLE>
<P>
</DIV>
<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-2-SECT-6.1">The boolean Type</A></h3>

<P CLASS=para>
<tt CLASS=literal>boolean</tt> values are not integers, may not be treated
as integers, and may never be cast to or from any other
type.  To perform C-style conversions between a
<tt CLASS=literal>boolean</tt> value <tt CLASS=literal>b</tt> and an <tt CLASS=literal>int</tt>
<tt CLASS=literal>i</tt>, use the following code:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
b = (i != 0);   // integer-to-boolean: non-0 -&gt; true; 0 -&gt; false; 
i = (b)?1:0;    // boolean-to-integer: true -&gt; 1; false -&gt; 0;
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-2-SECT-6.2">The char Type</A></h3>

<P CLASS=para>
<tt CLASS=literal>char</tt> values represent characters.  Character
literals may appear in a Java program between single
quotes.  For example:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
char c = 'A';
</PRE>
</DIV>

<P CLASS=para>
All of the standard C character escapes, as well as Unicode
escapes, are also supported in character literals.  For
example:

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
char newline = '\n', apostrophe = '\", delete = '\377', aleph='\u05D0';
</PRE>
</DIV>

<P CLASS=para>
Values of type <tt CLASS=literal>char</tt> do not have a sign.  If a
<tt CLASS=literal>char</tt> is cast to a <tt CLASS=literal>byte</tt> or a <tt CLASS=literal>short</tt>, a
negative value may result.

<P CLASS=para>
The <tt CLASS=literal>char</tt> type in Java holds a two-byte Unicode
character.  While this may seem intimidating to those not
familiar with Unicode and the techniques of program
internationalization, it is in fact totally transparent.
Java does not provide a way to compute the size of a
variable, nor does it allow any sort of pointer arithmetic.
What this means is that if you are only using ASCII or Latin-1
characters, there is no way to distinguish a Java
<tt CLASS=literal>char</tt> from a C <tt CLASS=literal>char</tt>.

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-2-SECT-6.3">Integral Types</A></h3>

<P CLASS=para>
The integral types in Java are <tt CLASS=literal>byte</tt>, <tt CLASS=literal>short</tt>,
<tt CLASS=literal>char</tt>, <tt CLASS=literal>int</tt>, and <tt CLASS=literal>long</tt>.  Literals for
these types are written just as they are in C.
All integral types, other than <tt CLASS=literal>char</tt>, are signed.
There is no <tt CLASS=literal>unsigned</tt> keyword as there is in C.
It is not legal to write <tt CLASS=literal>long</tt> <tt CLASS=literal>int</tt> or
<tt CLASS=literal>short</tt> <tt CLASS=literal>int</tt> as it is in C.
A <tt CLASS=literal>long</tt> constant may be
distinguished from other
integral constants by appending the character <tt CLASS=literal>l</tt> or
<tt CLASS=literal>L</tt> to it.  

<P CLASS=para>
Integer division by zero or modulo zero causes an
<tt CLASS=literal>ArithmeticException</tt> to be thrown.
[3]

<blockquote class=footnote>
<P CLASS=para>[3] 
Exceptions signal errors in Java.  Exception handling is described later in this chapter.
</blockquote>
</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-2-SECT-6.4">Floating-Point Types</A></h3>

<P CLASS=para>
The floating-point types in Java are <tt CLASS=literal>float</tt> and
<tt CLASS=literal>double</tt>.  Literals for these types are written just
as they are in C.
Literals may be specified to be of type
<tt CLASS=literal>float</tt> by appending an <tt CLASS=literal>f</tt> or <tt CLASS=literal>F</tt> to the
value; they may be specified to be of type
<tt CLASS=literal>double</tt> by appending a <tt CLASS=literal>d</tt> or <tt CLASS=literal>D</tt>.

<P CLASS=para>
<tt CLASS=literal>float</tt> and <tt CLASS=literal>double</tt> types have special values
that may be the result of certain floating-point
operations: positive infinity, negative infinity, negative
zero and not-a-number.  The <tt CLASS=literal>java.lang.Float</tt> and
<tt CLASS=literal>java.lang.Double</tt> classes define some of these values
as constants: <tt CLASS=literal>POSITIVE_INFINITY</tt>,
<tt CLASS=literal>NEGATIVE_INFINITY</tt>, and <tt CLASS=literal>NaN</tt>.

<P CLASS=para>
<tt CLASS=literal>NaN</tt> is unordered--comparing it to any other number,
including itself, yields <tt CLASS=literal>false</tt>.  Use
<tt CLASS=literal>Float.isNaN()</tt> or <tt CLASS=literal>Double.isNaN()</tt> to test for
<tt CLASS=literal>NaN</tt>.
Negative zero compares equal to regular zero (positive zero),
but the two zeros may be distinguished by division: one
divided by negative zero yields negative infinity; one
divided by positive zero yields positive infinity.

<P CLASS=para>
Floating-point arithmetic never causes exceptions, even in
the case of division by zero.

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JNUT2-CH-2-SECT-6.5">String Literals</A></h3>

<P CLASS=para>
Strings in Java are not a primitive type, but are instances
of the <tt CLASS=literal>String</tt> class.  However, because they are so
commonly used, string literals may appear between quotes in
Java programs, just as they do in C.  When the compiler
encounters such a string literal, it automatically creates the
necessary <tt CLASS=literal>String</tt> object.

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_05.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch02_07.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Unicode and Character Escapes</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Reference Data Types</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
